<?php
namespace app\api\controller;

use app\common\controller\ApiBaseController;
use app\common\model\WsmessageModel;
use think\Validate;

class WsmsgController extends ApiBaseController
{
    public function index()
    {
        self::testGet();

        $data = self::check_data();
        dump($data);

        $model = new WsmessageModel;
    }

    private function check_data()
    {
        $init_data = ['siteid' => '0', 'columnid' => '0', 'name' => '', 'phone' => '', 'email' => '', 'desc' => '', 'ip' => ''];
        $param = $this->request->param();
        $param = array_merge($init_data, $param);
        //dump($param);
        $rule = [
            'wsmessage_siteid' => 'require|number|gt:0',
            'wsmessage_columnid' => 'require|number|gt:0',
            'wsmessage_name' => '',
            'wsmessage_phone' => 'number',
            'wsmessage_email' => 'email',
            'wsmessage_desc' => '',
        ];
        $msg = [
            'wsmessage_siteid.require' => '提示：siteid 必填！',
            'wsmessage_siteid.number' => '提示：siteid 必须为数字！',
            'wsmessage_siteid.gt' => '提示：siteid 必须大于 0！',
            'wsmessage_columnid.require' => '提示：columnid 必填！',
            'wsmessage_columnid.number' => '提示：columnid 必须为数字！',
            'wsmessage_columnid.gt' => '提示：columnid 必须大于 0！',
            'wsmessage_phone.number' => '提示：phone 必须为数字！',
            'wsmessage_phone.email' => '提示：email 必须为邮箱！',
        ];

        $data = [
            'wsmessage_siteid' => $param['siteid'],
            'wsmessage_columnid' => $param['columnid'],
            'wsmessage_name' => $param['name'],
            'wsmessage_phone' => $param['phone'],
            'wsmessage_email' => $param['email'],
            'wsmessage_desc' => $param['desc'],
            'wsmessage_ip' => '',
        ];

        $validate = Validate::make($rule, $msg);
        $result = $validate->check($data);
        if ($result) {
            return $this->error($validate->getError());
        }
        return $data;
    }
}
